package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyFocusAdapter;
import com.runen.infinitemovement.adapter.RerverLikeAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.DynamicsEntity;
import com.runen.infinitemovement.model.entity.MyFocusEntity;
import com.runen.infinitemovement.model.entity.RerverLikeEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReceiveLikedActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.top_btn_right)
    Button topBtnRight;
    @BindView(R.id.top_iv_right)
    ImageView topIvRight;
    @BindView(R.id.rv_receive_liked)
    RecyclerView rvReceiveLiked;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    @BindView(R.id.top_tv_right)
    TextView topTvRight;
    @BindView(R.id.v_title_line)
    View vTitleLine;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    private RerverLikeAdapter myFocusAdapter;
    private String dynamicId;
    private int page = 2;
    private int dynamicFlag;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_receive_liked;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        dynamicId = getIntent().getStringExtra("dynamicId");
        dynamicFlag = getIntent().getIntExtra("dynamicFlag", 0);
        topTitle.setText("收到的赞");
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvReceiveLiked.setLayoutManager(layoutManager);
        myFocusAdapter = new RerverLikeAdapter(this);
        myFocusAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvReceiveLiked.setAdapter(myFocusAdapter);
        initData();

        myFocusAdapter.setOnItemFocusClickListener(new RerverLikeAdapter.OnItemFocusClickListener() {
            @Override
            public void addFocus(RerverLikeEntity.DataBean item, int position) {
                goAddFocus(item,position);
            }

            @Override
            public void cancelFocus(RerverLikeEntity.DataBean item, int position) {
                onCancelFocus(item,position);
            }
        });
        iniLoadMore();
    }

    private void iniLoadMore() {
        myFocusAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        myFocusAdapter.getLoadMoreModule().setAutoLoadMore(true);
        myFocusAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    /**
     * 关注
     *
     * @param item
     */
    private void goAddFocus(RerverLikeEntity.DataBean item,int position) {
        JSONObject object = new JSONObject();
        try {
            object.put("concernedUserId", item.userId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new UserAPI().addFocus(object.toString(), new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {
                if (response.getCode()==0){
                    ToastUtils.showShort("关注成功");
                }else {
                    ToastUtils.showShort(response.getMessage());
                }
                myFocusAdapter.notifyItemChanged(position);

            }

            @Override
            public void onComplete() {
                super.onComplete();

            }
        });

    }

    /**
     * 取消关注
     *
     * @param item
     */
    private void onCancelFocus(RerverLikeEntity.DataBean item,int position) {
        JSONObject object = new JSONObject();
        try {
            object.put("unConcernedUserId", item.userId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new UserAPI().cancelFocus(object.toString(), new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {
                if (response.getCode()==0){
                    ToastUtils.showShort("取消关注成功");
                }else {
                    ToastUtils.showShort(response.getMessage());
                }
                myFocusAdapter.notifyItemChanged(position);
            }

            @Override
            public void onComplete() {
                super.onComplete();

            }
        });

    }


    private void initData() {
        srl.setRefreshing(true);
        String str;
        if (dynamicFlag == 0) {
            str = "?dynamicId=%s&limit=20&page=1";
        } else {
            str = "?informationId=%s&limit=20&page=1";
        }
        new ActivityAPI().getReceiveLikedList(String.format(str, dynamicId), dynamicFlag, new BaseResultCallback<RerverLikeEntity>() {
            @Override
            public void onResponse(RerverLikeEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        if (response.data.size() >= response.count) {
                            myFocusAdapter.setNewData(response.data);
                            if (myFocusAdapter.getData().size() >= response.count) {
                                myFocusAdapter.getLoadMoreModule().loadMoreEnd(true);
                                myFocusAdapter.getLoadMoreModule().setEnableLoadMore(false);
                            }
                        }
                    }
                }
            }

            @Override
            public void onError(Throwable e, String body) {
                super.onError(e, body);
                srl.setRefreshing(false);
            }

            @Override
            public void onComplete() {
                super.onComplete();
                srl.setRefreshing(false);
            }
        });
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    public void loadMoreRequested() {
        String str;
        if (dynamicFlag == 0) {
            str = "?dynamicId=%s&limit=20&page=%s";
        } else {
            str = "?informationId=%s&limit=20&page=%s";
        }

        new ActivityAPI().getReceiveLikedList(String.format(str, dynamicId, page), dynamicFlag, new BaseResultCallback<RerverLikeEntity>() {
            @Override
            public void onResponse(RerverLikeEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        myFocusAdapter.addData(response.data);
                        if (myFocusAdapter.getData().size() >= response.count) {
                            myFocusAdapter.getLoadMoreModule().loadMoreEnd(true);
                            myFocusAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            myFocusAdapter.getLoadMoreModule().loadMoreComplete();
                            myFocusAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }
            }
        });
    }


    @OnClick(R.id.top_btn_left)
    public void onViewClicked() {
        finish();
    }
}
